﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Avaliacao.Scenes
{
    class Level_1 : BaseScene
    {
        protected Tower selectedTower;
        protected TowerManager tower_manager;

        int[,] map = {{0,0,1,0,0,0,0,0,0,0,0,0,0,0,-1,-1}, 
                      {0,0,1,0,0,0,0,0,0,0,0,0,0,0,-1,-1},
                      {0,0,1,0,0,0,0,0,0,0,0,0,0,0,-1,-1},
                      {0,0,1,1,1,1,1,1,1,1,1,0,0,0,-1,-1},
                      {0,0,0,0,0,0,0,0,0,0,1,0,0,0,-1,-1},
                      {0,0,0,0,0,0,0,0,0,0,1,0,0,0,-1,-1},
                      {0,0,0,0,0,0,0,0,0,0,1,1,1,0,-1,-1},
                      {0,0,0,0,0,0,0,0,0,0,0,0,1,0,-1,-1},
                      {0,0,0,0,0,0,0,0,0,0,0,0,1,0,-1,-1},
                      {0,0,0,0,0,1,1,1,1,1,1,1,1,0,-1,-1},
                      {0,0,0,0,0,1,0,0,0,0,0,0,0,0,-1,-1},
                      {0,0,0,0,0,1,0,0,0,0,0,0,0,0,-1,-1}};

        

        public Level_1(ContentManager Content, GameWindow Window, TowerManager tower_manager): base(Content, Window)
        {
            this.selectedTower = null;
            this.tower_manager = tower_manager;
        }

        public override void Update(GameTime gameTime, KeyboardState keyboard, KeyboardState last_keyboard, MouseState mouse, MouseState last_mouse)
        {
            Tower hoverTower = null; //Torre na qual o mouse se encontra em cima.
            int mouseColumn = (int)Math.Floor(mouse.X / TILE);
            int mouseRow = (int)Math.Floor(mouse.Y / TILE);

            hoverTower = tower_manager.Update(gameTime, keyboard, last_keyboard, mouse, last_mouse);
                            
            if (selectedTower != null)
            {
                selectedTower.Position = new Vector2(mouseColumn * TILE, mouseRow * TILE);

                //Checa se a região do mapa está livre (valor zero)
                if (map[mouseRow, mouseColumn] == 0)
                    selectedTower.setAnimation((int)Tower.animIndexes.Still);
                else
                    selectedTower.setAnimation((int)Tower.animIndexes.Blocked);
            }

            
            if (mouse.LeftButton == ButtonState.Released && last_mouse.LeftButton == ButtonState.Pressed)
            {
                //Cliquei em cima de uma torre no menu sem ter nenhuma selecionada
                if (selectedTower == null && hoverTower != null)
                {
                    selectedTower = hoverTower.clone(new Vector2(mouse.X, mouse.Y));
                }
                //Tenho uma torre selecionada seguindo o mouse
                else if (selectedTower != null)
                {
                    //Só podemos colocar a torre se sua animação não estiver como bloqueada
                    if (selectedTower.getAnimationIndex() != (int)Tower.animIndexes.Blocked)
                    {
                        map[mouseRow, mouseColumn] = 2; //2 - significa que existe uma torre no local
                        tower_manager.addTower(selectedTower);

                        selectedTower = null;
                    }
                }
            }   
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < MAP_HEIGHT; i++)
            {
                for (int j = 0; j < MAP_WIDTH; j++)
                {
                    if (map[i, j] == 0)
                    {
                        spriteBatch.Draw(Game1.grass, new Vector2(j * 50, i * 50), Color.White);
                    }
                    else if (map[i, j] == 1)
                    {
                        spriteBatch.Draw(Game1.road, new Vector2(j * 50, i * 50), Color.White);
                    }
                }
            }

            tower_manager.Draw(gameTime, spriteBatch);
            
            if (selectedTower != null)
                selectedTower.Draw(gameTime, spriteBatch, selectedTower.getAnimation());   
        }
    }
}
